package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.feign.ApiSearchFeignClient;
import com.atguigu.gmall.web.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/***
 * 前端搜索控制层
 */
@Controller
@RequestMapping("/page/search")
public class SearchController {

    @Value("${item.url}")
    private String itemUrl;

    @Autowired
    private ApiSearchFeignClient apiSearchFeignClient;

    /**
     * 跳转到商品列表页面
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping
    public String search(@RequestParam Map<String,Object> searchParam, Model model){
        //1 远程调用servicer-list微服务的接口
        Map result = apiSearchFeignClient.search(searchParam);
//        System.out.println("result = " + result);

        //2 将搜索出来的结果放入请求域
        model.addAllAttributes(result);

        //3 将整个searchParam放入请求域
        model.addAttribute("searchParam",searchParam);

        //4 获取当前请求完成以后的url
        String requestUrl = this.getRequestUrl(searchParam);
        model.addAttribute("requestUrl",requestUrl);

        //5 获取排序的url
        String sortRequestUrl = this.getSortRequestUrl(searchParam);
        model.addAttribute("sortRequestUrl",sortRequestUrl);

        //6 分页信息
        //获取总数据量
        Object totalHits = result.get("totalHits");
        //获取有效页码
        int pageNum = this.getPageNum((String) searchParam.get("pageNum"));
        //声明分页工具类对象
        Page pageInfo = new Page<>(Long.valueOf(totalHits.toString()),pageNum,50);
        //放入请求域
        model.addAttribute("pageInfo",pageInfo);

        //7 保存商品详情页的前缀
        model.addAttribute("itemUrl",itemUrl);

        return "list";
    }

    /**
     * 返回有效页码
     *
     * @param pageNum
     * @return
     */
    private static int getPageNum(String pageNum) {
        try{
            int pageN = Integer.parseInt(pageNum);
            //防止负数
            pageN = pageN <= 0 ? 1 : pageN;
            //防止超出范围
            pageN = pageN>199?199:pageN;
            //返回
            return pageN;
        }catch (Exception e){
            return 1;
        }
    }

    /**
     * 拼接排序、分页的url
     * @param searchParam
     * @return
     */
    private String getSortRequestUrl(Map<String, Object> searchParam) {
        String url = "/page/search?";
        //遍历
        for (Map.Entry<String, Object> param : searchParam.entrySet()) {
            String key = param.getKey();
            String value = (String) param.getValue();
            if(key.equals("sortField") ||
                    key.equals("sortRule") ||
                    key.equals("pageNum")){
                continue;
            }
            url += key + "=" + value + "&";
        }

        //去除尾部&
        url = url.substring(0, url.length() - 1);

        return url;
    }

    /**
     * 拼接url
     * @param searchParam
     * @return
     */
    private String getRequestUrl(Map<String, Object> searchParam) {
        String url = "/page/search?";

        //遍历
        for (Map.Entry<String, Object> param : searchParam.entrySet()) {
            String key = param.getKey();
            String value = (String) param.getValue();
            if(!key.equals("pageNum")){
                url += key + "=" + value + "&";
            }
        }
        //去除尾部&
        url = url.substring(0, url.length() - 1);

        return url;
    }
}
